//
//  Director.cpp
//  Prism
//
//  Created by Bas du Pre on 19-05-11.
//  Copyright 2011 UU. All rights reserved.
//

#include <iostream>
#include "graphics/Director.h"

#include "graphics/Sprite.h"

using namespace Prism;
using namespace Prism::Graphics;

// Allocating and initializing Director's
// static data member.  The pointer is being
// allocated - not the object inself.
Director *Director::s_instance = 0;

Director *Director::instance()
{
    if (!s_instance) {
        PRISM_LOGINFO("Creating Director instance...");
        s_instance = new Director;
        s_instance->init();
    }
    
    return s_instance;
}

Director::Director()
{
    PRISM_LOG_QUIET(String("Prism ") + PRISM_VERSIONSTRING);
    PRISM_LOG_QUIET(String("Compiled on: ") + PRISM_COMPILED_ON);
    
    _scene = 0;
    _maxFramerate = 60;
    
    native_init();        
}

void Director::init() {
    setShowDebugLayer(false);
    _debugLayer = new DebugLayer();
}

bool Director::isCreated() {
    return s_instance != 0;
}

void Director::resume() {
    
}

Director::~Director() {
    PRISM_LOGINFO("Director is destroying...");
    native_deInit();
}

void Director::runWithScene(Scene *scene) {
    _scene = scene;
    native_sceneUpdate();
}

Scene* Director::runningScene() {
    return _scene;
}

Prism::Size Director::winSize() {
    return _winSize;
}

void Director::setWinSize(Prism::Size size) {
    if (_winSize.width == size.width && _winSize.height == size.height)
        return;
    
    _winSize = size;
    EventArgs e;
    onWinSizeChanged(this, &e);
}

Director* Prism::Graphics::theDirector() {
    return Director::instance();
}

void Director::drawScene() {
    Node::_staticScreenZ = 0;
    
    native_beforeDrawScene();
    
    if (runningScene() != 0) runningScene()->visit();
    if (showDebugLayer()) _debugLayer->visit();
    
    native_afterDrawScene();
}

PFloat Director::fps() {
    return native_fps();
}

void Director::setShowDebugLayer(bool showDebugLayer) {
    _showDebugLayer = showDebugLayer;
}

bool Director::showDebugLayer() {
    return _showDebugLayer;
}

String Director::toString() {
    return "[Director]";
}

void Director::setMaxFrameRate(PInt maxFrameRate) {
    _maxFramerate = maxFrameRate;
}

PInt Director::maxFrameRate() {
    return _maxFramerate;
}

